package com.lemon.smartsafety.ui.fragment.notice;

import android.os.Bundle;

import com.lemon.smartsafety.app.AppService;
import com.lemon.smartsafety.app.BundleKey;
import com.lemon.smartsafety.app.EventTag;
import com.lemon.smartsafety.app.RefreshType;
import com.lemon.smartsafety.entity.event.common.PhoneCallEvent;
import com.lemon.smartsafety.entity.event.notice.NoticeRefreshEvent;
import com.lemon.smartsafety.entity.notice.NoticeReaderVo;
import com.lemon.smartsafety.http.service.NoticeServices;
import com.lemon.smartsafety.listener.RecyclerTClickListener;
import com.lemon.smartsafety.ui.adapter.notice.NoticeSentDetailAdapter;
import com.lemon.smartsafetynew.ui.fragment.base.BasePullToRefreshFragment;
import com.lemon.smartsafety.utils.common.StringUtils;
import com.lemon.smartsafety.view.list.MultiStateView;
import com.zhy.http.okhttp.OkHttpUtils;

import org.greenrobot.eventbus.Subscribe;

import java.util.List;

import kale.adapter.LemonRcvAdapter;

/**
 * Created by Cooliean on 16/4/8.
 * 已发公告中的公告详情List 可以看到公告的阅读人员状态
 */
public class NoticeSentDetailListFragment extends BasePullToRefreshFragment<NoticeReaderVo, List<NoticeReaderVo>> implements RecyclerTClickListener<NoticeReaderVo> {
    private String                  mKeyWord  = "";
    private String                  sNoticeId = "";
    private NoticeServices.ReadType mReadType = NoticeServices.ReadType.UN_READ;


    public static NoticeSentDetailListFragment newInstance(String text, NoticeServices.ReadType readType, String noticeId) {
        Bundle bundle = new Bundle();
        bundle.putString(BundleKey.KEY_WORD, StringUtils.nullValue(text));
        bundle.putInt(BundleKey.HAS_READ, readType.getValue());
        bundle.putString(BundleKey.NOTICE_ID, StringUtils.nullValue(noticeId));
        NoticeSentDetailListFragment pageFragment = new NoticeSentDetailListFragment();
        pageFragment.setArguments(bundle);
        return pageFragment;
    }


    @Override
    protected void getBaseArguments() {
        super.getBaseArguments();
        if (getArguments() != null) {
            mKeyWord = getArguments().getString(BundleKey.KEY_WORD);
            sNoticeId = getArguments().getString(BundleKey.NOTICE_ID);
            int readTypeValue = getArguments().getInt(BundleKey.HAS_READ, NoticeServices.ReadType.UN_READ.getValue());
            if (readTypeValue == NoticeServices.ReadType.UN_READ.getValue()) {
                mReadType = NoticeServices.ReadType.UN_READ;
            } else {
                mReadType = NoticeServices.ReadType.READ;
            }
        }
        mKeyWord = StringUtils.nullValue(mKeyWord);
        sNoticeId = StringUtils.nullValue(sNoticeId);
    }

    @Override
    public boolean goRefreshOnCreate() {
        return true;
    }

    @Override
    public LemonRcvAdapter<NoticeReaderVo> getLemonAdapter() {
        return new NoticeSentDetailAdapter(mDataList, this);
    }

    @Override
    public String getHttpTag() {
        return EventTag.TAG_NOTICE_SENT_DETAIL_LIST;
    }

    @Override
    public void onLoadMore() {
        NoticeServices.init()
                      .getSentNoticeDetailList(RefreshType.LOAD_MORE, pageSize, currentPage, sNoticeId, mReadType, mKeyWord);
    }

    @Override
    public void onPullRefresh() {
        NoticeServices.init()
                      .getSentNoticeDetailList(RefreshType.REFRESH, pageSize, currentPage, sNoticeId, mReadType, mKeyWord);
    }


    @Override
    public void onItemClick(final NoticeReaderVo data, int position) {
        String phoneNumber = StringUtils.nullValue(data.getMobile());
        if (phoneNumber.length() != 0) {
            PhoneCallEvent event = new PhoneCallEvent();
            event.setTag(NoticeSentDetailListFragment.class.getName());
            event.setPhone(phoneNumber);
            AppService.getBus().post(event);
        }
    }


    @Subscribe
    public void onEventMainThread(NoticeRefreshEvent event) {
        mMultiStateView.setViewState(MultiStateView.VIEW_STATE_CONTENT);
        mPullToRefreshRecyclerView.beginRefreshing();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        OkHttpUtils.getInstance().cancelTag(getHttpTag());
    }
}
